package dontCare.gwtester.client.gwt.leakyAbstraction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import dontCare.gwtester.client.tool.SourceUrl;
import dontCare.gwtester.client.tool.UnitWidget;

public class ArrayOutOfBound extends UnitWidget{
	private static ArrayOutOfBoundUiBinder uiBinder = GWT.create(ArrayOutOfBoundUiBinder.class);
	interface ArrayOutOfBoundUiBinder extends UiBinder<Widget, ArrayOutOfBound> {}

	@UiField Label arrayString;
	@UiField Label element5;
	@UiField Label exception;
	
	public ArrayOutOfBound() {
		initWidget(uiBinder.createAndBindUi(this));
		
		int[] array = {1,2,3,4};
		arrayString.setText(Arrays.toString(array));
		try{
			element5.setText(""+array[5]);
		}catch(Exception e){
			exception.setText(e.toString());
		}
	}

	@Override
	public List<String> getSourceUrl() {
		ArrayList<String> result = new ArrayList<String>();
		result.add(SourceUrl.genJava(this.getClass()));
		result.add(SourceUrl.genUiXml(this.getClass()));
		return result;
	}

	@Override
	public HTML getDescription() {
		return new HTML("以直接指定 index 的方式讀取超出陣列範圍的 element，預期會有 ArrayIndexOutOfBoundsException，而在 Development Mode 也如預期。" +
				"但是在 Product Mode 時卻不會有 exception，而會得到 undefine。" +
				"<br/>這在 GWT 模擬 java.lang.ArrayIndexOutOfBoundsException 的 source code 註解有提到 ＝＝\"。");
	}

	@Override
	public String getName() {
		return "超出陣列長度問題";
	}
}
